package Routers

import (
	"fmt"
	"github.com/golang/protobuf/proto"
	"server5/Dao"
	"server5/Interface"
	"server5/protobuf"
	"server5/utils"
)

type Register struct{}


func (Register) Func(request Interface.IRequest) {

	if request.GetConnection().User().Status() != utils.NotLogin {
		fmt.Println("不处理注册消息")
		return
	}

	fmt.Println("开始处理注册消息")

	var (
		registerMsg protobuf.RegisterMsg
		registerResultMsg protobuf.RegisterResultMsg
	)

	proto.Unmarshal(request.GetData(),&registerMsg)
	ok, _ := Dao.GetUserDao().AddUserInfo(registerMsg.UserName,registerMsg.UserPwd)
	if ok {
		registerResultMsg.Flag = true
		registerResultMsg.Result = "注册成功"
		//Debug
		fmt.Println("注册成功")
	} else {
		registerResultMsg.Flag = false
		registerResultMsg.Result = "用户名已被占用"
		//Debug
		fmt.Println("用户名已被占用")
	}
	request.GetConnection().SendMsg(protobuf.MsgType_RegisterResult,&registerResultMsg)
	return
}